#!/usr/bin/env python
# -*- coding: utf-8 -*-

from odoo import models, fields, api

class QualityRejectWizard(models.TransientModel):
    _name = 'quality.reject.wizard'
    _description = 'Quality Rejection Wizard'

    check_id = fields.Many2one('quality.check', required=True)
    product_id = fields.Many2one('product.product', required=True)
    line_ids = fields.One2many(
        'quality.reject.wizard.line', 
        'wizard_id',
        string='Rejection Lines'
    )

    def action_confirm_rejection(self):
        self.ensure_one()
        # Create reject reasons
        for line in self.line_ids:
            self.env['quality.reject.reason'].create({
                'check_id': self.check_id.id,
                'product_id': self.product_id.id,
                'quantity': line.quantity,
                'reason_code': line.reason_code,
                'description': line.description
            })
        # Create return picking with quantities
        self.check_id.create_return_picking()
        # Close wizard
        return {'type': 'ir.actions.act_window_close'}

class QualityRejectWizardLine(models.TransientModel):
    _name = 'quality.reject.wizard.line'
    _description = 'Rejection Wizard Line'

    wizard_id = fields.Many2one('quality.reject.wizard')
    #  reason_code = fields.Selection(
        #  related='wizard_id.product_id.reject_reason_selection',
        #  string='Reason Code',
        #  required=True
    #  )
    reason_code = fields.Many2one('product.iqc.reject.reason.line', 'Reason', required=True)
    quantity = fields.Float(
        'Quantity',
        digits='Product Unit of Measure',
        required=True
    )
    description = fields.Char('Additional Notes')
